package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.ApplyQingjiaActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.RuleResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.eqdd.yiqidian.ui.select.SelectRichTextActivity;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ApplyQingjiaActivity extends SlimBaseMultiEditActivity {
    private ApplyQingjiaActivityCustom dataBinding;


    private Bundle bundle;

    private ActionSheetDialog chooseDialog;
    @ToSave
    RichTextResult richTextResult;
    private SimpleDateFormat sf;
    private String msg;

    @Override
    public void initBinding() {
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_apply_qingjia);
        initTopTitleBar(View.VISIBLE, "请假申请");
        initTopRightText("提交", v -> {
            if (checkParam()) {
                PostRequest<HttpResult> params = OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_LEAVE)
                        .params("userGuid", user.getGuid())
                        .params("companyId", user.getCompanyId())
                        .params("leadvStartTime", setParam(4))
                        .params("leaveEndTime", setParam(5))
                        .params("leaveTime", setParam(6))
                        .params("leaveType", setParam(7))
                        .params("leaveReason", setParam(8));
                if (richTextResult != null) {
                    List<LocalMedia> selectMedias = richTextResult.getSelectMedia();
                    for (int i = 0, lenth = selectMedias.size(); i < lenth; i++) {
                        LocalMedia localMedia = richTextResult.getSelectMedia().get(i);
                        params.params("file" + i, new File(localMedia.isCompressed() ?
                                localMedia.getCompressPath() : localMedia.getPath()));
                    }
                }
                params.execute(new DialogCallBack<HttpResult>(ApplyQingjiaActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            isSave=false;
                            clearData();
                            setResult(Config.SUCCESS);
                            finish();
                        }
                    }
                });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(setParam(4))) {
            msg = "请选择开始时间";
            return false;
        }
        if (TextUtils.isEmpty(setParam(5))) {
            msg = "请选择结束时间";
            return false;
        }
        if (TextUtils.isEmpty(setParam(6))) {
            msg = "请输入请假天数";
            return false;
        }
        if (TextUtils.isEmpty(setParam(7))) {
            msg = "请选择请假类型";
            return false;
        }
        if (TextUtils.isEmpty(setParam(8))) {
            msg = "请输入请假原因";
            return false;
        }
        if ((setParam(7).equals("婚假") || setParam(7).equals("产假")) && (richTextResult.getSelectMedia() == null || richTextResult.getSelectMedia().size() == 0)) {
            msg = setParam(7) + "需要照片证明";
            return false;
        }
        return true;
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (p) {
            case 8:
                bundle = new Bundle();
                bundle.putSerializable(Config.RICH_TEXT_RESULT, richTextResult);
                bundle.putString(Config.TITLE, "请假原因");
                startActivityForResult(SelectRichTextActivity.class, bundle, Config.SELECT_RICH_TEXT);
                break;
            case 7:
                if (chooseDialog == null) {
                    SingleChooseUtil.getChooseDialog(ApplyQingjiaActivity.this, 33, (sheet) -> {
                        chooseDialog = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(p);
                    });
                } else {
                    chooseDialog.show();
                }
                break;
        }

    }

    @Override
    protected void onTimeClick(int p) {
        switch (p) {
            case 5:
            case 4:
                if (!TextUtils.isEmpty(slimAdapterEx.getDataItem(4).getContent()) && !TextUtils.isEmpty(slimAdapterEx.getDataItem(5).getContent())) {
                    try {
                        Date from = sf.parse(slimAdapterEx.getDataItem(4).getContent());
                        Date to = sf.parse(slimAdapterEx.getDataItem(5).getContent());
                        int day = (int) Math.ceil(Math.abs(((to.getTime() - from.getTime()) / (1000 * 60 * 60 * 24))));
                        slimAdapterEx.getDataItem(6).setContent(day + "");
                        slimAdapterEx.notifyItemChanged(6);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditHintBean("姓名", user.getUsername(), 0));
        datas.add(new SlimEditHintBean("工号", user.getJobNumber(), 1));
        datas.add(new SlimEditHintBean("部门", user.getDepartment(), 2));
        datas.add(new SlimEditHintBean("职位", user.getPost(), 3));
        datas.add(new SlimEditTimeBean("请假开始时间", "", 4, "请选择").setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE));
        datas.add(new SlimEditTimeBean("请假结束时间", "", 5, "请选择").setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE));
        datas.add(new SlimEditEnterBean("请假天数", "", 6, "请输入").addSchemes(StaticScheme.Numeric()));
        datas.add(new SlimEditEnterBean("请假类型", "", 7, true, "请选择"));
        datas.add(new SlimEditEnterBean("请假原因", "", 8, true, "请输入"));
        datas.add(new SlimEditHintBean("审核人", "", 9));
    }

    @Override
    protected void loadNetData() {
        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.GET_LEADER)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<String>>(ApplyQingjiaActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.getData().get(9).setContent(httpResult.getItems());
                            slimAdapterEx.notifyItemChanged(9);
                        }
                    }
                });
        OkGo.<HttpResult<RuleResultBean>>post(HttpConfig.BASE_URL + HttpConfig.MY_RULE)
                .params("userGuid", user.getGuid())
                .params("companyId", user.getCompanyId())
                .execute(new DialogCallBack<HttpResult<RuleResultBean>>(ApplyQingjiaActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<RuleResultBean>> response) {
                        HttpResult<RuleResultBean> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.SELECT_RICH_TEXT:
                    richTextResult = (RichTextResult) data.getSerializableExtra(Config.RICH_TEXT_RESULT);
                    slimAdapterEx.getData().get(8).setContent(richTextResult.getContent());
                    slimAdapterEx.notifyItemChanged(8);
                    break;
            }

        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
